<?php
/**
 * Yotaku
 * @author Creatint<creatint@163.com>
 * @copyright www.yotaku.cn 2016
 */
namespace Addons\Wechat;
use Common\Controller\Addon;
use Common\Tool\Wechat;
use Home\Model\User;
class WechatAddon extends Addon{
    public $info = array(
        'name'=>'Wechat',
        'title'=>'微信插件',
        'description'=>'实现微信相关功能',
        'status'=>1,
        'author'=>'Creatint',
        'version'=>'0.1'
    );

    /**
     *  微信登录与自动登录
     * @param bool $direct false:不跳转，返回boolean/uid  true:跳转
     * @return mixed
     */
    public function login($params=array('direct'=>false)){
        trace("微信登录插件");
//        dump("微信登录插件");
        // 登录成功的跳转页面
        $acturl = I('acturl',U('Center/index'),'strip_tags');

        //获取uid
        $uid = session("member.id");
        //是否存在UID
        if($uid>0){
            if($params['direct']){
                //直接跳转
                $this->successAlert('登录成功！',$acturl,$encode=true);
            } else {
                //返回boolean
                return $uid;
            }
        }

        //判断是否是微信端
        if ( strpos($_SERVER['HTTP_USER_AGENT'], 'MicroMessenger') !== false ) {
            trace("微信端");
//            dump("微信端");
        }else{
            trace("非微信端");
//            dump("非微信端");
            if ( !C('PC_ALLOW') ){
                echo '请使用微信打开';
                exit;
            }
            return false;
        }

        // 无UID，需要凭微信token登录
        // 微信配置
        $config = $this->getConfig();
//        dump($config);
//        $options = array(
// 			'token'=>'ofEvnnxo54KDcESHzboK', //填写你设定的key
// 			'encodingaeskey'=>'PpwAqWgAFHNzrFdfxNuxLQFWzWOUJWTaRXGBvpHvNbY', //填写加密用的EncodingAESKey
// 			'appid'=>'wxdcb96e770144f0be', //填写高级调用功能的app id
// 			'appsecret'=>'e8045c388d96468ab1ff1dbf936bb4bd' //填写高级调用功能的密钥
//        );
        $weObj = new Wechat($config);
//        $weObj->valid();
//        dump($weObj);
        // 授权跳,获取 code
        if(!$_GET['code']){
//            trace("不存在code");
            $url = $weObj->getOauthRedirect($callback="http://".$_SERVER["HTTP_HOST"].$_SERVER['PHP_SELF'],$state='',$scope='snsapi_base');
//            echo '获取code的url ';dump($url);
            redirect($url);
            exit;
        }else{
            //获取access_token及openid
            $res = $weObj->getOauthAccessToken();
            trace($res);
            session("wechat",$res);

            //凭openid登录
            $User = D("User");
            $map['openid'] = $res['openid'];
            if($member = $User->where($map)->find()){
//                echo 'member= ';dump($member);
                session("member",$member);
            }else{
                dump("不存在openid");
                return array('direct'=>$params['direct'],'openid'=>$res['openid']);
            }

        }
    }


    public function login_model($info){
        if(!$info){
            return false;
        }
        if($_REQUEST['openid']){
            $User = D("User");
            $data['openid'] = $_REQUEST['openid'];
            $User->where("id={$info['id']}")->save($data);
        }
    }



    public function install(){
        return true;
    }

    public function uninstall(){
        return true;
    }
}